<?php

namespace Services\User;

use Common\Model\User\UserViewLogModel;
use Services\BaseService;

class UserViewLogService extends BaseService
{
    /** @var UserViewLogModel $model */
    private $model;

    public function __construct()
    {
        $this->model = new UserViewLogModel();
    }

    /**
     * 添加浏览记录
     * @return false|float|int|mixed|string
     */
    public function addRecord()
    {
        $viewSecond = $this->getParam('view_second', UserViewLogModel::DEFAULT_VIEW_SECOND);
        $type = $this->getParam('type');
        $viewAllTime = M("user_view_log")->where([
            'user_id' => $this->getParam('user_id', 0),
            'type' => $type,
            'view_user_id' => $this->getParam('view_user_id', 0),
            'model_id' => $this->getParam('model_id', 0),
        ])->sum("view_second");
        return $this->model->add([
            'user_id' => $this->getParam('user_id', 0),
            'type' => $type,
            'view_user_id' => $this->getParam('view_user_id', 0),
            'model_id' => $this->getParam('model_id'),
            'view_day' => date('Y-m-d'),
            'view_second' => $viewSecond,
            'view_all_time' => $viewAllTime + $viewSecond,
            'created_at' => get_now_date(),
            'updated_at' => get_now_date(),
        ]);
    }

    /**
     * 获取某一类型的所有浏览用户
     * @param $type
     * @param $modelId
     * @return mixed
     */
    public function getViewAllUser()
    {
        $baseWhere = [];
        if ($viewUserId = $this->getParam('view_user_id')) {
            $baseWhere['v.view_user_id'] = $viewUserId;
        }
        if ($modelId = $this->getParam('model_id')) {
            $baseWhere['v.model_id'] = $modelId;
        }
        if($type = $this->getParam('type')){
            $baseWhere['v.type'] = $type;
        }
        $users = $this->model
            ->alias('v')
            ->join('left join ' . C('DB_PREFIX') . 'user' . ' u on u.id = v.user_id')
            ->where($baseWhere)
            ->field("v.*,u.user_headimg")
            ->order("update_time desc")
            ->group("v.user_id");

        if ($limit = $this->getParam('limit')) {
            $users->limit($limit);
        }
        return $users->select();
    }

    /**
     * 查询今天的浏览人
     * @param $type
     * @param $modelId
     * @param $limit
     * @return mixed
     */
    public function getTodayViewUser($userId)
    {
//        $users = M("user_view_log")
//            ->alias('v')
//            ->join('left join ' . C('DB_PREFIX') . 'user' . ' u on u.id = v.user_id')
//            ->where([
////                'type' => $type,
//                'model_id' => $modelId,
//                'view_day' => date('Y-m-d'),
//            ])
//            ->field("v.*,u.user_headimg")
//            ->order("update_time desc")
//            ->group("user_id")
//            ->limit($limit)
//            ->select();
        $users = M("user_view_log")
            ->where(['view_user_id' => $userId, 'view_day' => date("Y-m-d")])
            ->group("user_id")
            ->select();
        return $users;
    }

    public function getTodayViewNumber($type, $modelId)
    {
        return $this->model->where([
            'view_day' => date('Y-m-d'),
            'model_id' => $modelId,
        ])->count();
    }

    /**
     * 获取浏览的数量
     * @param $type
     * @param $modelId
     * @return mixed
     */
    public function getViewNumber($type, $modelId)
    {
        return $this->model->where([
            'type' => $type,
            'model_id' => $modelId,
        ])->count();
    }

}
